#!/usr/bin/env python
# -*- coding: utf-8 -*-
from scrapy import signals
from facility.log_fn import logger
import os


class OpenCloseLogging(object):

    @classmethod
    def from_crawler(cls, crawler):
        ext = cls()
        crawler.signals.connect(ext.spider_opened, signal=signals.spider_opened)
        crawler.signals.connect(ext.spider_closed, signal=signals.spider_closed)
        return ext

    def spider_opened(self, spider):
        logger("opened spider %s" % spider.name, 'pid:', os.getpid())

    def spider_closed(self, spider):
        logger("closed spider %s" % spider.name)
